from Lexer import Lexer
from sys import exit
from sys import stdout
from sys import stdin

class Parser:
	def __init__(self,l):
		self.lex = l
		self.t = None
		
	def parse(self):
		self.t = self.lex.nextToken()
		self._expr()
		print "PRINT"
		if not (stdin.isatty()):
			if (stdin.read(1)):
				print "ERROR"
		
	def _expr(self):
		self._term()
			
		if self.t.tCode == "PLUS":
			self.t = self.lex.nextToken()
			self._expr()
			print "ADD"

	def _term(self):
		self._factor()

		if self.t.tCode == "MULT":
			self.t = self.lex.nextToken()
			self._term()
			print "MULT"

			
	def _factor(self):
		if self.t.tCode == "INT":
			print "PUSH " + self.t.lexeme
			self.t = self.lex.nextToken()

		elif self.t.tCode == "LPAREN":
			self.t = self.lex.nextToken()
			self._expr()

			if self.t.tCode != "RPAREN":
				self._synError()
			self.t = self.lex.nextToken()

		else:
			self._synError()
			
	def _synError(self):
		print "Syntax error"
		exit()
	